package itbour.onetouchshow.widget.clip;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import itbour.onetouchshow.App;
import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.utils.UIUtils;


public class ClipImageLayout extends RelativeLayout {


    //用于绘制的compoundPath
    CompoundPathsBean compoundPathsBean;
    private Point point;
    private ClipZoomImageView zoomImg;

    public ClipImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        point = new Point();


    }

    public void setData(CompoundPathsBean compoundPathsBean, Bitmap bitmap) {
        this.compoundPathsBean = compoundPathsBean;
        WindowManager windowManager = (WindowManager) App.context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getRealSize(point);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int windowW = dm.widthPixels;
        int windowH = dm.heightPixels - UIUtils.dp2px(50);
        ViewGroup.LayoutParams lp = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                windowH);


//        if (compoundPathsBean != null) {
//
//
//            //计算clip layout中的宽高
//            float pathW = compoundPathsBean.getW();
//            float pathH = compoundPathsBean.getH();
//            double scaleHeight = windowW / pathW;
//            double scaleWidth = windowH / pathH;
//            double minScale = scaleHeight > scaleWidth ? scaleWidth : scaleHeight;//缩放比例
//
//            int height = (int) (pathH * minScale);
//            int width = (int) (pathW * minScale);
//
//            //这个控件是含手势缩放的图片
//            zoomImg = new ClipZoomImageView(getContext());
//            zoomImg.setImageBitmap(bitmap);
//            zoomImg.setClipWH(width, height);
//
//            //这是蒙板
//            CompoundPathImageView cPathImg = new CompoundPathImageView(getContext());
//            cPathImg.setData(compoundPathsBean, windowW, windowH);
//
//            this.addView(cPathImg, lp);
//        } else {
//            //这个控件是含手势缩放的图片
//            zoomImg = new ClipZoomImageView(getContext());
//            zoomImg.setImageBitmap(bitmap);
//
//            int width = bitmap.getWidth();
//            int height = bitmap.getHeight();
//
//            zoomImg.setClipWH(width, height);
//        }


//        //计算clip layout中的宽高
        float pathW = bitmap.getWidth();
        float pathH = bitmap.getHeight();
        double scaleHeight = windowW / pathW;
        double scaleWidth = windowH / pathH;
        double minScale = scaleHeight > scaleWidth ? scaleWidth : scaleHeight;//缩放比例

        int width = (int) (pathW * minScale);
        int height = (int) (pathH * minScale);
        //这个控件是含手势缩放的图片
        zoomImg = new ClipZoomImageView(getContext());
        zoomImg.setImageBitmap(bitmap);
        //这里传入的是当前图片显示的区域的宽高
        zoomImg.setClipWH(width, height);
        this.addView(zoomImg, lp);
    }

    /**
     * 裁切图片 实际调用zoomImg的clip
     *
     * @return
     */
    public Bitmap clip() {
        return zoomImg.clip();
    }

    public Bitmap getBitMap() {
        return zoomImg.getOriangeBitMap();
    }


}

